<template>
  <div class="dict">
    <div class="dictList">
      <div class="list-div">
        <el-row>
          <el-col :lg="4" :md="6">
            <el-button type="success" size="small" plain @click="handleSave('saveModal')">新 增</el-button>
            <el-button type="danger" size="small" plain @click="deleteBatch()">删 除</el-button>
          </el-col>
          <el-col :lg="10" :md="10">
            <el-col :lg="10" :md="10">
              <el-input
                size="small"
                @keyup.enter.native="search"
                placeholder="名称/字典KEY"
                v-model="searchFilter.condition"
                clearable
              ></el-input>
            </el-col>
            <el-col :offset="1" :span="6">
              <el-button size="small" type="primary" @click="search" plain>查 询</el-button>
            </el-col>
          </el-col>
        </el-row>

        <br>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="40" header-align="center"></el-table-column>
          <el-table-column type="index" width="40" header-align="center"></el-table-column>

          <el-table-column sortable prop="typeName" label="名称" align="center"></el-table-column>
          <el-table-column sortable prop="dictKey" label="字典KEY" align="center"></el-table-column>
          <el-table-column sortable prop="dictValue" align="center" label="字典值"></el-table-column>
          <el-table-column sortable prop="enable" align="center" label="是否启用">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.enable"
                active-value="1"
                inactive-value="0"
                active-color="#13ce66"
                inactive-color="#C0C0C0"
                @change="changeStatus(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column sortable prop="createTime" align="center" label="创建时间"></el-table-column>
          <el-table-column sortable prop="updateTime" align="center" label="更新时间"></el-table-column>
          <el-table-column fixed="right" label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click.native="handleEdit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination block">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page.sync="page"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total"
          ></el-pagination>
        </div>
      </div>
    </div>
    <div class="saveModal">
      <el-dialog :title="saveName" :visible.sync="showSaveModal" @close="cancelR" width="45%">
        <el-form
          :rules="saveModalRules"
          ref="saveModal"
          :model="saveModal"
          label-width="120px"
          size="small"
        >
          <br>
          <el-row>
            <el-col :span="20">
              <el-form-item label="名称" prop="typeName">
                <el-input v-model="saveModal.typeName"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="字典KEY" prop="dictKey">
                <el-input v-model="saveModal.dictKey"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <br>
          <el-row>
            <el-col :span="20">
              <el-form-item label="字典值" prop="dictValue">
                <el-input v-model="saveModal.dictValue"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <br>
          <br>
          <el-row>
            <el-col :span="20">
              <el-form-item size="large">
                <el-button type="primary" @click="save('saveModal')">{{saveBtnName}}</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "Role",
  data() {
    return {
      saveName: "",
      resourceCheckList: [1],
      showSetResourceModal: false,
      codeInputEnable: false,
      searchFilter: {
        condition: ""
      },
      saveBtnName: "创建字典",
      showSaveModal: false,
      setResourceRoleId: "",
      resourceList: [
        {
          id: "",
          name: "所有菜单",
          items: []
        }
      ],
      tableData: [],
      page: 1,
      pageSize: 10,
      total: 100,
      orgId: null,
      multipleSelection: [],
      saveModalRules: {
        typeName: [
          {
            required: true,
            message: "请输入字典名称",
            trigger: "blur"
          }
        ],
        dictKey: [
          {
            required: true,
            message: "请输入字典KEY",
            trigger: "blur"
          }
        ],
        dictValue: [
          {
            required: true,
            message: "请输入字典值",
            trigger: "blur"
          }
        ]
      },
      saveModal: {
        id: "",
        typeName: "", //字典名称
        dictKey: "", //字典KEY
        dictValue: "" //字典值
      },
      tmpSaveModal: {
        id: "",
        typeName: "", //字典名称
        dictKey: "", //字典KEY
        dictValue: "" //字典值
      },
      row: {
        id: "",
        typeName: "", //字典名称
        dictKey: "", //字典KEY
        dictValue: "" //字典值
      },
      defaultProps: {
        children: "items",
        label: "name"
      }
    };
  },
  mounted() {
    this.handleNodeClick();
  },
  methods: {
    //根据条件查询列表
    search() {
      let qColumn;
      let qValue;
      if (
        this.searchFilter.condition != null &&
        this.searchFilter.condition.length > 0
      ) {
        qColumn = "AND_type_name_LK,OR_dict_key_LK";
        qValue =
          this.searchFilter.condition + "," + this.searchFilter.condition;
      }
      this.http
        .post("/api/dict/query", {
          P_NO: this.page,
          P_SIZE: this.pageSize,
          P_ORDER: "DESC",
          P_SORT: "create_time",
          Q_COLUMN: qColumn != null ? qColumn : "",
          Q_VALUE: qValue != null ? qValue : ""
        })
        .then(res => {
          var data = res.data;
          this.tableData = data.rows;
          this.total = data.records;
        });
    },
    //新增事件
    handleSave(formName) {
      this.saveBtnName = "创建字典";
      this.saveModal = this.tmpSaveModal;
      this.saveName = "创建字典";
      this.showSaveModal = true;
      this.codeInputEnable = false;
    },
    handleCurrentChange(val) {
      this.page = val;
      this.handleNodeClick();
    },
    handleEdit(row) {
      this.row.id = row.id;
      this.row.typeName = row.typeName;
      this.row.dictKey = row.dictKey;
      this.row.dictValue = row.dictValue;

      this.saveModal = row;
      this.saveName = "编辑字典";
      this.showSaveModal = true;
      this.codeInputEnable = true;
      this.saveBtnName = "保存字典";
    },
    //关闭弹框右上角
    cancelR() {
      if (this.saveName == "编辑字典") {
        this.saveModal.id = this.row.id;
        this.saveModal.typeName = this.row.typeName;
        this.saveModal.dictKey = this.row.dictKey;
        this.saveModal.dictValue = this.row.dictValue;
        this.$refs.saveModal.clearValidate();
      }
      if (this.saveName == "创建字典") {
        this.$refs.saveModal.clearValidate();
        (this.saveModal.id = ""),
          (this.saveModal.typeName = ""),
          (this.saveModal.dictKey = ""),
          (this.saveModal.dictValue = "");
      }
    },

    save(formName) {
      let _this = this;
      this.$refs[formName].validate(valid => {
        if (!valid) {
          console.log("error submit!!");
          return false;
        }
      });
      if (
        this.saveModal.typeName &&
        this.saveModal.dictKey &&
        this.saveModal.dictValue
      ) {
        this.http.post("/api/dict/save", this.saveModal).then(res => {
          let data = res.data;
          if (data.code === 200) {
            this.$message({
              showClose: true,
              message: data.msg,
              type: "success"
            });
            _this.showSaveModal = false;
            _this.handleNodeClick();
          } else {
            this.$message.error(data.msg);
          }
        });
      }
    },
    //批量删除
    deleteBatch() {
      let selectDataList = this.multipleSelection;
      if (selectDataList.length == 0) {
        this.$message({
          message: "请选中一条项再操作",
          type: "warning"
        });
        return;
      }
      let ids = "";
      for (let i = 0; i < selectDataList.length; i++) {
        if (i != selectDataList.length - 1) {
          ids += selectDataList[i].id + ",";
        } else {
          ids += selectDataList[i].id;
        }
      }
      this.$confirm("此操作将永久删除选中的数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.http
          .post("/api/dict/deleteBatchIds", {
            ids: ids
          })
          .then(res => {
            let data = res.data;

            if (data.code === 200) {
              this.$message({
                showClose: true,
                message: data.msg,
                type: "success"
              });
              this.handleNodeClick();
            } else {
              this.$message.error(data.msg);
            }
          });
      });
    },
    changeStatus(row) {
      this.http
        .post("/api/dict/save", {
          id: row.id,
          enable: row.enable
        })
        .then(res => {
          let data = res.data;
          if (data.code === 200) {
            this.$message({
              showClose: true,
              message: data.msg,
              type: "success"
            });
          }
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleNodeClick() {
      this.http
        .post("/api/dict/query", {
          P_NO: this.page,
          P_SIZE: this.pageSize,
          P_ORDER: "DESC",
          P_SORT: "create_time"
        })
        .then(res => {
          var data = res.data;
          this.tableData = data.rows;
          this.total = data.records;
        });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.dict {
  padding: 0 10px;
  .pagination {
    float: right;
  }
}

.face-div {
  margin: 0 auto;
  align-items: center;
  justify-content: center;
  text-align: center;

  .item {
    display: inline-block;
    box-shadow: 0 0 4px black;
  }

  img {
    width: 200px;
    height: auto;
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
